<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta http-equiv="X-UA-Compatible" content="IE=edge">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <title>使括号有效的最少添加</title>
</head>
<body>
  <script>
    function fn(S) {
      var res = []
      for (let i of S) {
        if (res[0] == '(' && i == ')') {
          res.shift()
        } else {
          res.unshift(i)
        }
      }
      return res.length
    }
    console.log(fn('())'))
  </script>
</body>
</html>